const { Backend, Joi } = require('dx-router-admin');

module.exports = class Permission extends Backend {
	constructor(ctx) {
		super(ctx);
		this.dto = {
			permission_id: Joi.string()
				.required()
				.error((errors) => {
					return new Error(ctx.$t('权限ID不能为空'));
				}),
			permission_name: Joi.string()
				.required()
				.error(new Error(ctx.$t('权限名称不能为空'))),
		};
	}
	// 批量操作
	async adds({ comment, parent_id, permission_list, permission_type }) {
		if (!Array.isArray(permission_list)) {
			return this.$dx.fail('参数格式错误');
		}
		for (let item of permission_list) {
			await this.currentService.insert({
				comment,
				parent_id,
				permission_type,
				permission_id: item.value,
				permission_name: item.label,
			});
		}
		return this.$dx.success('操作成功');
	}
	/**
	 * 权限生成
	 */
	async createPermission(params) {
		const { $dx } = this;
		// 第一个是父
		if (!params[0]) {
			return $dx.fail('参数格式错误');
		}
		// 对象或数组
		await this.currentService.createPermission(params[0]);
		return $dx.success('操作成功');
	}
};
